<template>
  <view class="container">
    <!-- 用户信息 -->
    <view v-if="loginUser" class="userinfo">
      <view class="userinfo-con" @click="openUserInfo">
        <view class="userinfo-avatar">
          <image :src="
              userInfo.avatar
                ?
                  (userInfo.avatar.indexOf('http') === -1 ? picDomain + userInfo.avatar : userInfo.avatar)
                :
                  '/static/images/icon/head04.png'
            " />
        </view>
        <view class="userinfo-name">
          <view>{{ userInfo.nickname ? userInfo.nickname : "用户昵称" }}</view>
        </view>
      </view>
    </view>

    <view v-if="!loginUser" class="userinfo-none">
      <view class="default-pic" @tap="toLogin">
        <image src="@/static/images/icon/head04.png" />
      </view>
      <view class="none-login" @tap="toLogin">
        <button class="unlogin">
          未登录
        </button>
        <button class="click-login">
          点击登录账号
        </button>
      </view>
    </view>
    <!-- end 用户信息 -->

    <view class="list-cont">
      <!-- 订单状态 -->
      <view class="total-order">
        <view class="order-tit">
          <text style="font-weight:bold">
            我的交易
          </text>
        </view>
        <view class="procedure">
          <view class="items" @tap="toMyGoodsPage">
            <image src="@/static/images/icon/myGoods.png" />
            <text>我发布的</text>
          </view>
          <view class="items" data-orderType="1" @tap="toOrderListPage">
            <image src="@/static/images/icon/mySale.png" />
            <text>我卖出的</text>
          </view>
          <view class="items" data-orderType="2" @tap="toOrderListPage">
            <image src="@/static/images/icon/toTake.png" />
            <text>我买到的</text>
          </view>
        </view>
      </view>
      <!--end 订单状态 -->

      <view class="my-menu">
        <view class="memu-item" @tap="toAddressList">
          <view class="i-name">
            <image src="@/static/images/icon/myAddr.png" />
            <text>收货地址</text>
          </view>
          <view class="arrowhead" />
        </view>


        <view class="memu-item" @tap="toBalance">
          <view class="i-name">
            <image src="@/static/images/icon/balance.png" />
            <text>我的余额</text>
          </view>
          <view class="arrowhead" />
          </view>
          <view class="memu-item" @tap="toAbout">
            <view class="i-name">
              <image src="@/static/images/icon/about.png" />
              <text>关于我们</text>
            </view>
            <view class="arrowhead" />

        </view>



        <!--end 列表项 -->

        <view v-if="loginUser" class="log-out" @tap="logout">
          <view class="log-out-n">
            <text>退出登录</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
  import http from '../../utils/http';
  const picDomain = import.meta.env.VITE_APP_RESOURCES_URL
  const loginUser = ref('')
  const userInfo = ref('')
  /**
   * 生命周期函数--监听页面显示
   */
  onShow(() => {
    loginUser.value = uni.getStorageSync("Token")
    if (loginUser.value != '') {
      console.log(loginUser);
      console.log("uuuuuu");
      getUserInfo()
    }
  })

  // 获取用户信息
  const getUserInfo = () => {
    http.request({
      url: '/user/getUserInfo',
      method: 'GET',
    }).then((res) => {
      console.log("huoq");
      console.log(res);
      userInfo.value = res.data
      console.log(userInfo.value);
    })
  }

  const openUserInfo = () => {
    uni.navigateTo({
      url: '/pages/UserInfo/UserInfo'
    })
  }

  const toAddressList = () => {
    uni.navigateTo({
      url: '/pages/delivery-address/delivery-address'
    })
  }

  const toOrderListPage = (e) => {
    const orderType = e.currentTarget.dataset.orderType
    if (orderType == 1) {
      uni.navigateTo({
        url: '/pages/mySaleOrderList/mySaleOrderList?sts=0'
      })
    } else {
      uni.navigateTo({
        url: '/pages/myBuyOrderList/myBuyOrderList?sts=0'
      })
    }
  }

  const toMyGoodsPage = () => {
    uni.navigateTo({
      url: '/pages/myGoods/myGoods'
    })
  }

  const toBalance = () => {
    uni.navigateTo({
      url: '/pages/balance/balance'
    })
  }

  const toAbout = () => {
    uni.navigateTo({
      url: '/pages/about/about'
    })
  }

  /**
   * 去登陆
   */
  const toLogin = () => {
    uni.navigateTo({
      url: '/pages/accountLogin/accountLogin'
    })
  }

  /**
   * 退出登录
   */
  const logout = () => {
    http.request({
        url: '/logout',
        method: 'post'
      })
      .then(() => {
        util.removeTabBadge()
        uni.removeStorageSync('Token')
        uni.removeStorageSync('userInfo')
        uni.removeStorageSync('refreshToken')
        loginUser.value = ''
        uni.showToast({
          title: '退出成功',
          icon: 'none'
        })
        setTimeout(() => {
          uni.switchTab({
            url: '/pages/index/index'
          })
        }, 1000)
      })
  }
</script>

<style scoped lang="scss">
  @use './user.scss';
</style>
